﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace TimViec.NguoiDung
{
    public partial class TaoHoSoBuoc2 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if(!IsPostBack)
            {
                if(Request.QueryString["hoso"]!="")
                {
                    var hosoId = Convert.ToInt32(Request.QueryString["hoso"]);
                    LoadData(hosoId);
                }
                else
                {
                    Response.Redirect("/");
                }
            }
        }

        private void LoadData(int hosoId)
        {
            if (Session["NguoiDung"] != null)
            {

                DAL.User user = BLL.NguoiDung.NguoiDung.Get_UserEmail(Session["NguoiDung"].ToString());
                if (user.tbl_HS_HoSos.Any(r => r.HoSoId == hosoId))
                {
                    droHocVan.DataSource = BLL.Admin.HocVanHelper.List_DMHoVan();
                    droHocVan.DataTextField = "MoTa";
                    droHocVan.DataValueField = "HocVanId";
                    droHocVan.DataBind();
                    ListItem item3 = new ListItem();
                    item3.Text = "Chọn học vấn";
                    item3.Value = "-1";
                    item3.Selected = true;
                    droHocVan.Items.Add(item3);

                    droChuyenMon.DataSource = BLL.Admin.NganhNgheHelper.List_NganhNghe_ValueConfig(1);
                    droChuyenMon.DataTextField = "Ten";
                    droChuyenMon.DataValueField = "NganhNgheId";
                    droChuyenMon.DataBind();

                    ListItem item1 = new ListItem();
                    item1.Text = "Chọn chuyên môn";
                    item1.Value = "-1";
                    item1.Selected = true;
                    droChuyenMon.Items.Add(item1);

                    droQuocGia.DataSource = BLL.Admin.QuocGiaHelper.List_QuocGia();
                    droQuocGia.DataTextField = "Ten";
                    droQuocGia.DataValueField = "QuocGiaId";
                    droQuocGia.DataBind();

                    ListItem item2 = new ListItem();
                    item2.Text = "Chọn quốc gia";
                    item2.Value = "-1";
                    item2.Selected = true;
                    droQuocGia.Items.Add(item2);
                    //Load common

                    var ngonNgus = BLL.Admin.DMNgonNguHelper.ListNgonNgu();
                    droNgoaiNgu1.DataSource = ngonNgus;
                    droNgoaiNgu1.DataTextField = "Ten";
                    droNgoaiNgu1.DataValueField = "NgonNguId";
                    droNgoaiNgu1.DataBind();
                    ListItem item4 = new ListItem();
                    item4.Text = "Chọn ngôn ngữ";
                    item4.Value = "-1";
                    item4.Selected = true;
                    droNgoaiNgu1.Items.Add(item4);


                    droNgoaiNgu2.DataSource = ngonNgus;
                    droNgoaiNgu2.DataTextField = "Ten";
                    droNgoaiNgu2.DataValueField = "NgonNguId";
                    droNgoaiNgu2.DataBind();
                    droNgoaiNgu2.Items.Add(item4);

                    droNgoaiNgu3.DataSource = ngonNgus;
                    droNgoaiNgu3.DataTextField = "Ten";
                    droNgoaiNgu3.DataValueField = "NgonNguId";
                    droNgoaiNgu3.DataBind();
                    droNgoaiNgu3.Items.Add(item4);

                    var hocvans = BLL.NguoiDung.HoSo.LoadHocVan(hosoId);
                    rptHocVan.DataSource = hocvans;
                    rptHocVan.DataBind();

                    var ngoaiNgu = BLL.NguoiDung.HoSo.LoadNgoaiNgu(hosoId);
                    if (ngoaiNgu != null)
                    {
                        hdngonnguId.Value = ngoaiNgu.Id.ToString();

                        if (ngoaiNgu.NgoaiNgu1 != null)
                            droNgoaiNgu1.SelectedValue =
                                ngonNgus.First(r => r.Ten == ngoaiNgu.NgoaiNgu1).NgonNguId.ToString();
                        if (ngoaiNgu.NgoaiNgu2 != null)
                            droNgoaiNgu2.SelectedValue =
                                ngonNgus.First(r => r.Ten == ngoaiNgu.NgoaiNgu2).NgonNguId.ToString();
                        if (ngoaiNgu.NgoaiNgu3 != null)
                            droNgoaiNgu3.SelectedValue =
                                ngonNgus.First(r => r.Ten == ngoaiNgu.NgoaiNgu3).NgonNguId.ToString();
                    }
                }
                else
                {
                    Response.Redirect("/");
                }
            }
            else
            {
                Response.Redirect("/");
            }
        }

        protected void btSave_Click(object sender, EventArgs e)
        {
            var hosoId = Convert.ToInt32(Request.QueryString["hoso"]);

            if (droNgoaiNgu1.SelectedValue == "-1")
            {
                ScriptManager.RegisterStartupScript(this, this.GetType(), "redirectMe", "alert('Chọn ngoại ngữ!');", true);
                droNgoaiNgu1.Focus();
                return;
            }

            if (droCapDo1.SelectedValue == "-1")
            {
                ScriptManager.RegisterStartupScript(this, this.GetType(), "redirectMe", "alert('Chọn cấp độ!');", true);
                droCapDo1.Focus();
                return;
            }



            string nn1, nn2, nn3;
            nn1 = droNgoaiNgu1.SelectedValue == "-1" ? null : droNgoaiNgu1.SelectedItem.Text;
            nn2 = droNgoaiNgu2.SelectedValue == "-1" ? null : droNgoaiNgu2.SelectedItem.Text;
            nn3 = droNgoaiNgu3.SelectedValue == "-1" ? null : droNgoaiNgu3.SelectedItem.Text;
            if (hdngonnguId.Value == "0")
            {
                BLL.NguoiDung.HoSo.AddNgoaiNgu(hosoId, nn1, Convert.ToInt32(droCapDo1.SelectedValue), nn2,
                                               Convert.ToInt32(droCapDo2.SelectedValue), nn3,
                                               Convert.ToInt32(droCapDo3.SelectedValue));

                
            }
            else
            {
                BLL.NguoiDung.HoSo.UpdateNgoaiNgu(Convert.ToInt32(hdngonnguId.Value),nn1,
                                                  Convert.ToInt32(droCapDo1.SelectedValue), nn2,
                                                  Convert.ToInt32(droCapDo2.SelectedValue), nn3,
                                                  Convert.ToInt32(droCapDo3.SelectedValue));
            }
            Response.Redirect("/nguoi-dung/tu-ho-so.html");
        }

        protected void btSaveAndContinue_Click(object sender, EventArgs e)
        {
            var hosoId = Convert.ToInt32(Request.QueryString["hoso"]);

            if (droNgoaiNgu1.SelectedValue == "-1")
            {
                ScriptManager.RegisterStartupScript(this, this.GetType(), "redirectMe", "alert('Chọn ngoại ngữ!');", true);
                droNgoaiNgu1.Focus();
                return;
            }

            if (droCapDo1.SelectedValue == "-1")
            {
                ScriptManager.RegisterStartupScript(this, this.GetType(), "redirectMe", "alert('Chọn cấp độ!');", true);
                droCapDo1.Focus();
                return;
            }

            string nn1, nn2, nn3;
            nn1 = droNgoaiNgu1.SelectedValue == "0" ? null : droNgoaiNgu1.Text;
            nn2 = droNgoaiNgu2.SelectedValue == "0" ? null : droNgoaiNgu2.Text;
            nn3 = droNgoaiNgu3.SelectedValue == "0" ? null : droNgoaiNgu3.Text;
            if (hdngonnguId.Value == "0")
            {
                BLL.NguoiDung.HoSo.AddNgoaiNgu(hosoId, nn1, Convert.ToInt32(droCapDo1.SelectedValue), nn2,
                                               Convert.ToInt32(droCapDo2.SelectedValue), nn3,
                                               Convert.ToInt32(droCapDo3.SelectedValue));


            }
            else
            {
                BLL.NguoiDung.HoSo.UpdateNgoaiNgu(Convert.ToInt32(hdngonnguId.Value), nn1,
                                                  Convert.ToInt32(droCapDo1.SelectedValue), nn2,
                                                  Convert.ToInt32(droCapDo2.SelectedValue), nn3,
                                                  Convert.ToInt32(droCapDo3.SelectedValue));
            }
            Response.Redirect("/nguoi-dung/" + hosoId + "tao-ho-so-buoc3.html");
        }

        protected void btSaveHocVan_Click(object sender, EventArgs e)
        {
            var hosoId = Convert.ToInt32(Request.QueryString["hoso"]);

            if (droHocVan.SelectedValue == "-1")
            {
                ScriptManager.RegisterStartupScript(this, this.GetType(), "redirectMe", "alert('Chọn học vấn!');", true);
                droHocVan.Focus();
                return;
            }

            if (txtTruong.Text == "-1")
            {
                ScriptManager.RegisterStartupScript(this, this.GetType(), "redirectMe", "alert('Nhập tên trường!');", true);
                txtTruong.Focus();
                return;
            }

            if (droChuyenMon.SelectedValue == "-1")
            {
                ScriptManager.RegisterStartupScript(this, this.GetType(), "redirectMe", "alert('Chọn chuyên môn!');", true);
                droChuyenMon.Focus();
                return;
            }

            if (droQuocGia.SelectedValue == "-1")
            {
                ScriptManager.RegisterStartupScript(this, this.GetType(), "redirectMe", "alert('Chọn quốc gia!');", true);
                droQuocGia.Focus();
                return;
            }

            if (txtNgayBatDau.Text == "")
            {
                ScriptManager.RegisterStartupScript(this, this.GetType(), "redirectMe", "alert('Nhập ngày bắt đầu!');", true);
                txtNgayBatDau.Focus();
                return;
            }
            if (txtNgayKetThuc.Text == "")
            {
                ScriptManager.RegisterStartupScript(this, this.GetType(), "redirectMe", "alert('Nhập ngày kết thúc!');", true);
                txtNgayKetThuc.Focus();
                return;
            }

            if (txtMoTa.Text == "")
            {
                ScriptManager.RegisterStartupScript(this, this.GetType(), "redirectMe", "alert('Nhập quá trình học vấn!');", true);
                txtMoTa.Focus();
                return;
            }
            BLL.NguoiDung.HoSo.AddHocVan(hosoId, Convert.ToInt32(droHocVan.SelectedValue), txtTruong.Text,
                                         Convert.ToInt32(droChuyenMon.SelectedValue),
                                         Convert.ToInt32(droQuocGia.SelectedValue)
                                         , DateTime.Parse(txtNgayBatDau.Text), DateTime.Parse(txtNgayKetThuc.Text),
                                         txtMoTa.Text);
            var hocvans = BLL.NguoiDung.HoSo.LoadHocVan(hosoId);
            rptHocVan.DataSource = hocvans;
            rptHocVan.DataBind();
        }

        protected void rptHocVan_ItemCommand(object source, RepeaterCommandEventArgs e)
        {
            if(e.CommandName=="Xoa")
            {
                var hosoId = Convert.ToInt32(Request.QueryString["hoso"]);
                int hocvanId = int.Parse(e.CommandArgument.ToString());
                BLL.NguoiDung.HoSo.XoaHocVan(hocvanId);
                var hocvans = BLL.NguoiDung.HoSo.LoadHocVan(hosoId);
                rptHocVan.DataSource = hocvans;
                rptHocVan.DataBind();
            }
        }
    }
}